﻿/* Autoduel Game
 * CS4911 Senior Design Project
 * Fall 2010 Semester
 * Ian Axelrod, Liz Ha, Andrew Muldowney, Edward Futch
 */

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using AutoduelClient.GameClient.Player;
using AutoduelClient.GameUI;

using Microsoft.Xna.Framework;

namespace AutoduelClient
{
    public partial class Screen : Form
    {
		#region Fields (2)
		// reference to the game
        private GameUI.Game game;

		// used by XNA to for backbuffer width & height
        private Point drawSurfaceDim;

        private Boolean isEntered;

        public Boolean IsEntered
        {
            get { return isEntered; }
            set { isEntered = value; }
        }
		#endregion

		#region Properties (2)
		/// <summary>
        /// Get/Set the dimensions of the pctScreen surface
        /// </summary>
        public Point DrawSurfaceDim
        {
            get { return drawSurfaceDim; }
            set { drawSurfaceDim = value; }
        }
		/// <summary>
        /// Get/Set the game
        /// </summary>
        public GameUI.Game Game
        {
            get { return game; }
            set { game = value; }
        }
		#endregion

		#region Constructors (1)
		/// <summary>
        /// Constructs the main game client screen presented
        /// to the user
        /// </summary>
        public Screen()
        {
            InitializeComponent();
        }
		#endregion

		#region Methods (4)

		public IntPtr GetDrawSurface()
        {
            return pctScreen.Handle; 
        }

		/// <summary>
        /// Get the player/car speed
        /// </summary>
        /// <returns>The player/car speed</returns>
        public double GetSpeed()
        {
            return double.Parse(this.speed.Text);
        }

		/// <summary>
        /// Set the player health
        /// </summary>
        /// <param name="health">The player health to set</param>
        public void SetHealth(int health)
        {
            this.health.Text = health.ToString();
        }

		/// <summary>
        /// Set the player/car speed
        /// </summary>
        /// <param name="speed">The speed to set</param>
        public void SetSpeed(double speed)
        {
            this.speed.Text = String.Format("{0:0.##}", speed) + " mph"; 
            this.accBar.Value = (int)speed;
        }

		#endregion

		#region Event Handlers (5)

		private void Screen_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

		private void Screen_Load(object sender, EventArgs e)
        {
            this.adminToolStripMenuItem.Visible = false;
            this.drawSurfaceDim = new Point(this.pctScreen.Width, this.pctScreen.Height);
        }

		private void banPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.game.Client.World.BeginGetAllPlayersFromServer();
            // we can't freeze the game while we wait, otherwise we
            // will never dequeue the server messages
            while (!this.game.Client.World.ServerPlayersReady) { this.game.Tick(); }
            String playerName = InputBox.ShowPlayerList("Ban Player - Please enter a player name", new List<Player>(this.game.Client.World.ServerPlayers.Values));

            if (playerName != null)
            {
                this.game.Client.BanPlayerByName(playerName);
            }
        }

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

		private void kickPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String playerName = InputBox.ShowPlayerList("Kick Player - Please enter a player name", new List<Player>(this.game.Client.World.Players.Values));

            if (playerName != null)
            {
                this.game.Client.KickPlayerByName(playerName);
            }
        }

		#endregion


        private void Screen_Activated(object sender, EventArgs e)
        {
            isEntered = true;
        }

        private void Screen_Deactivate(object sender, EventArgs e)
        {
            isEntered = false;
        }
        public delegate DialogResult MessageBoxDelegate(String text, String title, MessageBoxButtons buttons, MessageBoxIcon icon);

        public DialogResult ShowMessageBox(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            object[] args = { message, caption, buttons, icon };
            return (DialogResult)this.Invoke(new MessageBoxDelegate(MessageBox.Show), args);
        }
    }
} 
